package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.MedicineDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineStorageDetailDAO;

import cn.edu.scau.cmi.hombio.domain.Medicine;
import cn.edu.scau.cmi.hombio.domain.MedicineStorage;
import cn.edu.scau.cmi.hombio.domain.MedicineStorageDetail;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for MedicineStorageDetail entities
 * 
 */

@Service("MedicineStorageDetailService")

@Transactional
public class MedicineStorageDetailServiceImpl implements MedicineStorageDetailService {

	/**
	 * DAO injected by Spring that manages Medicine entities
	 * 
	 */
	@Autowired
	private MedicineDAO medicineDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorage entities
	 * 
	 */
	@Autowired
	private MedicineStorageDAO medicineStorageDAO;

	/**
	 * DAO injected by Spring that manages MedicineStorageDetail entities
	 * 
	 */
	@Autowired
	private MedicineStorageDetailDAO medicineStorageDetailDAO;

	/**
	 * Instantiates a new MedicineStorageDetailServiceImpl.
	 *
	 */
	public MedicineStorageDetailServiceImpl() {
	}

	/**
	 */
	@Transactional
	public MedicineStorageDetail findMedicineStorageDetailByPrimaryKey(Integer id) {
		return medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(id);
	}

	/**
	 * Delete an existing MedicineStorageDetail entity
	 * 
	 */
	@Transactional
	public void deleteMedicineStorageDetail(MedicineStorageDetail medicinestoragedetail) {
		medicineStorageDetailDAO.remove(medicinestoragedetail);
		medicineStorageDetailDAO.flush();
	}

	/**
	 * Delete an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public MedicineStorageDetail deleteMedicineStorageDetailMedicineStorage(Integer medicinestoragedetail_id, Integer related_medicinestorage_id) {
		MedicineStorageDetail medicinestoragedetail = medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetail_id, -1, -1);
		MedicineStorage related_medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorage_id, -1, -1);

		medicinestoragedetail.setMedicineStorage(null);
		related_medicinestorage.getMedicineStorageDetails().remove(medicinestoragedetail);
		medicinestoragedetail = medicineStorageDetailDAO.store(medicinestoragedetail);
		medicineStorageDetailDAO.flush();

		related_medicinestorage = medicineStorageDAO.store(related_medicinestorage);
		medicineStorageDAO.flush();

		medicineStorageDAO.remove(related_medicinestorage);
		medicineStorageDAO.flush();

		return medicinestoragedetail;
	}

	/**
	 * Save an existing Medicine entity
	 * 
	 */
	@Transactional
	public MedicineStorageDetail saveMedicineStorageDetailMedicine(Integer id, Medicine related_medicine) {
		MedicineStorageDetail medicinestoragedetail = medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(id, -1, -1);
		Medicine existingmedicine = medicineDAO.findMedicineByPrimaryKey(related_medicine.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicine != null) {
			existingmedicine.setId(related_medicine.getId());
			existingmedicine.setName(related_medicine.getName());
			existingmedicine.setNumber(related_medicine.getNumber());
			existingmedicine.setCompany(related_medicine.getCompany());
			existingmedicine.setQuantityPerCase(related_medicine.getQuantityPerCase());
			existingmedicine.setQuantityPerBox(related_medicine.getQuantityPerBox());
			existingmedicine.setPhone(related_medicine.getPhone());
			existingmedicine.setMemo(related_medicine.getMemo());
			related_medicine = existingmedicine;
		}

		medicinestoragedetail.setMedicine(related_medicine);
		related_medicine.getMedicineStorageDetails().add(medicinestoragedetail);
		medicinestoragedetail = medicineStorageDetailDAO.store(medicinestoragedetail);
		medicineStorageDetailDAO.flush();

		related_medicine = medicineDAO.store(related_medicine);
		medicineDAO.flush();

		return medicinestoragedetail;
	}

	/**
	 * Save an existing MedicineStorage entity
	 * 
	 */
	@Transactional
	public MedicineStorageDetail saveMedicineStorageDetailMedicineStorage(Integer id, MedicineStorage related_medicinestorage) {
		MedicineStorageDetail medicinestoragedetail = medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(id, -1, -1);
		MedicineStorage existingmedicineStorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorage.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineStorage != null) {
			existingmedicineStorage.setId(related_medicinestorage.getId());
			existingmedicineStorage.setTime(related_medicinestorage.getTime());
			related_medicinestorage = existingmedicineStorage;
		}

		medicinestoragedetail.setMedicineStorage(related_medicinestorage);
		related_medicinestorage.getMedicineStorageDetails().add(medicinestoragedetail);
		medicinestoragedetail = medicineStorageDetailDAO.store(medicinestoragedetail);
		medicineStorageDetailDAO.flush();

		related_medicinestorage = medicineStorageDAO.store(related_medicinestorage);
		medicineStorageDAO.flush();

		return medicinestoragedetail;
	}

	/**
	 * Return a count of all MedicineStorageDetail entity
	 * 
	 */
	@Transactional
	public Integer countMedicineStorageDetails() {
		return ((Long) medicineStorageDetailDAO.createQuerySingleResult("select count(o) from MedicineStorageDetail o").getSingleResult()).intValue();
	}

	/**
	 * Return all MedicineStorageDetail entity
	 * 
	 */
	@Transactional
	public List<MedicineStorageDetail> findAllMedicineStorageDetails(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<MedicineStorageDetail>(medicineStorageDetailDAO.findAllMedicineStorageDetails(startResult, maxRows));
	}

	/**
	 * Delete an existing Medicine entity
	 * 
	 */
	@Transactional
	public MedicineStorageDetail deleteMedicineStorageDetailMedicine(Integer medicinestoragedetail_id, Integer related_medicine_id) {
		MedicineStorageDetail medicinestoragedetail = medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetail_id, -1, -1);
		Medicine related_medicine = medicineDAO.findMedicineByPrimaryKey(related_medicine_id, -1, -1);

		medicinestoragedetail.setMedicine(null);
		related_medicine.getMedicineStorageDetails().remove(medicinestoragedetail);
		medicinestoragedetail = medicineStorageDetailDAO.store(medicinestoragedetail);
		medicineStorageDetailDAO.flush();

		related_medicine = medicineDAO.store(related_medicine);
		medicineDAO.flush();

		medicineDAO.remove(related_medicine);
		medicineDAO.flush();

		return medicinestoragedetail;
	}

	/**
	 * Load an existing MedicineStorageDetail entity
	 * 
	 */
	@Transactional
	public Set<MedicineStorageDetail> loadMedicineStorageDetails() {
		return medicineStorageDetailDAO.findAllMedicineStorageDetails();
	}

	/**
	 * Save an existing MedicineStorageDetail entity
	 * 
	 */
	@Transactional
	public void saveMedicineStorageDetail(MedicineStorageDetail medicinestoragedetail) {
		MedicineStorageDetail existingMedicineStorageDetail = medicineStorageDetailDAO.findMedicineStorageDetailByPrimaryKey(medicinestoragedetail.getId());

		if (existingMedicineStorageDetail != null) {
			if (existingMedicineStorageDetail != medicinestoragedetail) {
				existingMedicineStorageDetail.setId(medicinestoragedetail.getId());
				existingMedicineStorageDetail.setTraceCode(medicinestoragedetail.getTraceCode());
				existingMedicineStorageDetail.setIsPutaway(medicinestoragedetail.getIsPutaway());
				existingMedicineStorageDetail.setIsUseout(medicinestoragedetail.getIsUseout());
			}
			medicinestoragedetail = medicineStorageDetailDAO.store(existingMedicineStorageDetail);
		} else {
			medicinestoragedetail = medicineStorageDetailDAO.store(medicinestoragedetail);
		}
		medicineStorageDetailDAO.flush();
	}
}
